package com.shiro.service.impl;

import com.shiro.mapper.RolePermissionMapper;
import com.shiro.pojo.Role;
import com.shiro.pojo.RolePermission;
import com.shiro.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;

public class RolePermissionServiceImpl implements RolePermissionService {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public void setPermissions(Role role, long[] permissionIds) {
        rolePermissionMapper.deleteByRid(role.getId());
        RolePermission rolePermission = new RolePermission();
        rolePermission.setRid(role.getId());
        for (long permissionId : permissionIds) {
            rolePermission.setPid(permissionId);
            rolePermissionMapper.insertSelective(rolePermission);
        }
    }

    @Override
    public void deleteByRole(long roleId) {
        rolePermissionMapper.deleteByRid(roleId);
    }

    @Override
    public void deleteByPermission(long permissionId) {
        rolePermissionMapper.deleteByPid(permissionId);
    }
}
